package com.by.common.core.controller;

import com.by.common.core.bean.ResultBean;
import com.by.common.core.exception.BusinessException;
import com.by.common.core.exception.ValidateException;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLSyntaxErrorException;

/**
 * 全局异常处理
 */
@RestControllerAdvice //异常的增强注解
public class GlobalExceptionController extends BaseController {


    /**
     * 当系统中 抛出了 ValidateException 异常 执行该方法
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ValidateException.class)
    public ResultBean handlerValidatException(ValidateException e) {
        e.printStackTrace();
        return fail("验证失败", e.getErrors());
    }

    /**
     * 处理sql语法 异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(SQLSyntaxErrorException.class)
    public ResultBean handlerSQLSyntaxErrorException(SQLSyntaxErrorException e) {
        e.printStackTrace();
        return fail("sql语法错误,请联系工程师！");
    }

    @ExceptionHandler(BusinessException.class)
    public ResultBean handlerBusinessException(BusinessException e) {
        e.printStackTrace();
        return fail(e.getMessage());
    }


}
